
const router = require('koa-router')();
const util = require("../common/utils")
const { sequelize } = require('../common/dbs')
// typeinfo
const ctypeModel = require("../models/ctype")
const ctypeinfo = ctypeModel(sequelize);

// 列表
router.get('/list', async (ctx) => {
    try {
        let data = await ctypeinfo.findAll(
            {
                attributes: {   // exclude排除
                    exclude: ['typeid', 'deletedAt', 'createdAt', 'updatedAt'],
                }
            }
        );
        ctx.body = util.resultSuccess({ data: data });
    } catch (err) {
        console.log(err);
        ctx.body = util.resultError({ data: null });
    }
})
// 列表
router.get('/admin/list', async (ctx) => {
    try {
        let data = await ctypeinfo.findAll(
            {
                attributes: {   // exclude排除
                    exclude: ['deletedAt', 'createdAt', 'updatedAt'],
                }
            }
        );
        ctx.body = util.resultSuccess({ data: data });
    } catch (err) {
        console.log(err);
        ctx.body = util.resultError({ data: null });
    }
})


/**
 * 修改
 * */
router.post('/editById', async (ctx) => {
    try {
        ctx
            .validateBody("id")
            .required("必须填写id")
            .isString()
            .trim()
        ctx
            .validateBody("typename")
            .required("标题不能为空")
            .isString()
            .trim()
        let reqBody = ctx.request.body;

        let requestData = await ctypeinfo.update({
            typename: reqBody.typename ? reqBody.typename : '',
            typenum: reqBody.typenum ? reqBody.typenum : '',
        }, {
            where: {
                id: reqBody.id
            }
        })
        if (requestData[0] > 0) {
            ctx.body = util.resultSuccess({ message: '修改成功', data: null });
        } else {
            ctx.body = util.resultError({ code: 0, message: '修改失败,id错误', data: null });
        }

    } catch (err) {
        console.log(err);
        ctx.body = util.resultError({ code: 0, message: err.message, data: null });
    }
})

/**
 * 新增
 * */
router.post('/add', async (ctx) => {
    try {
        ctx
            .validateBody("typename")
            .required("标题不能为空")
            .isString()
            .trim()

        let reqBody = ctx.request.body;
        let requestData = await ctypeinfo.create({
            typename: reqBody.typename ? reqBody.typename : '',
            typenum: reqBody.typenum ? reqBody.typenum : '',
        })
        if (requestData.dataValues) {
            ctx.body = util.resultSuccess({ message: '添加成功', data: requestData.dataValues });
        } else {
            ctx.body = util.resultSuccess({ code: 0, message: '添加失败', data: null });
        }
    } catch (err) {
        ctx.body = util.resultError({ code: 0, message: err.message, data: null });
    }
})

/**
 * 删除
 * */
router.post('/delById', async (ctx) => {
    try {
        ctx
            .validateBody("id")
            .required("id不能为空")
            .isString()
            .trim()

        let requestData = await ctypeinfo.destroy({
            where: {
                id: ctx.vals.id
            }
        })
        if (requestData > 0) {
            ctx.body = util.resultSuccess({ message: '删除成功' });
        } else {
            ctx.body = util.resultSuccess({ code: 0, message: '删除失败,该类型不存在', data: null });
        }
    } catch (err) {
        ctx.body = util.resultError({ code: 0, message: err.message, data: null });
    }
})

module.exports = router.routes(); 